package dungeons;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

import squares.Direction;
import squares.IllegalSquareException;
import squares.Square;
import squares.Square.TemperatureType;

import borders.*;

public class DungeonTest {
	
	private static Square square1,square2,square3,square4,square5,square6,square7,square8,square11,square10,square12;
	private static Plateau<Square> dungeon;
	
	
	@Before
	public void setUp() throws Exception {
		square1 = new Square(100,TemperatureType.CELSIUS, 50, false, false);
		square2 = new Square(100,TemperatureType.CELSIUS,50,false, true);
		square3 = new Square(100,TemperatureType.CELSIUS,50,false, false);
		square4 = new Square(100,TemperatureType.CELSIUS,50,false, true);
		square5 = new Square(100,TemperatureType.CELSIUS,50,false, false);
		square6 = new Square(100,TemperatureType.CELSIUS,50,false, false);
		square7 = new Square(100,TemperatureType.CELSIUS,50,false, true);
		square8 = new Square(100,TemperatureType.CELSIUS,50,false, true);
		square11 = new Square(100,TemperatureType.CELSIUS,50,false, true);
		square10 = new Square(100,TemperatureType.CELSIUS,50,false, false);
		square12 = new Square(100,TemperatureType.CELSIUS,50,false, true);
		square2.addTeleportableSquare(square4);
		square11.addTeleportableSquare(square2);
		square7.addTeleportableSquare(square2);
		square7.addTeleportableSquare(square11);
		square4.addTeleportableSquare(square7);
		dungeon = new Plateau<Square>(Long.MAX_VALUE,Long.MAX_VALUE);
		
		}
	@Test
	public void testCanGetTo() throws IllegalArgumentException, IllegalSquareException
	{
		
		dungeon.addSquare(square1, 23, 45, 0);
		dungeon.addSquare(square2, 23, 44, 0);
		square1.changeBorderAt(Direction.SOUTH, new Open(square1,square2));
		dungeon.addSquare(square3, 22,45 , 0);
		square3.changeBorderAt(Direction.EAST, new Door(square3,square1,true));
		dungeon.addSquare(square4, 123, 23, 0);
		dungeon.addSquare(square7, 10,14,0);
		dungeon.addSquare(square8, 10,15,0);
		square7.changeBorderAt(Direction.EAST, new Open(square7,square8));
		dungeon.addSquare(square11, 12, 2, 0);
		dungeon.addSquare(square10, 11, 2, 0);
		square10.changeBorderAt(Direction.EAST, new Open(square11,square10));
		assertTrue(Dungeon.canGetTo(square2, square10));
	}
	@Test
	public void testAddSquare() throws IllegalArgumentException, IllegalSquareException {
		dungeon.addSquare(square5, 1, 2, 0);
		assertEquals(square5,dungeon.getSquareAt(1, 2, 0));
		
	}
	@Test (expected = IllegalArgumentException.class)
	public void testAddSquare_IllegalCase() throws IllegalArgumentException, IllegalSquareException
	{
		dungeon.addSquare(square2, 1, 1, 1);
	}
	@Test (expected = IllegalSquareException.class)
	public void testAddSquare_IllegalCase2() throws IllegalArgumentException, IllegalSquareException
	{
		dungeon.addSquare(square1, 4, 24, 0);
		dungeon.addSquare(square1, 42, 1, 0);
	}

	@Test
	public void testDeleteSquare() throws IllegalArgumentException, IllegalSquareException {
		dungeon.addSquare(square6, 4, 3,0);
		dungeon.addSquare(square12, 5, 3,0);
		assert(((Square) dungeon.getSquareAt(5, 3, 0)).getBorderAt(Direction.EAST).getOtherAttachedSquare(square6).equals(square5));
		dungeon.deleteSquare(4, 3, 0);
		assert(dungeon.getAllSquares().size()==1);
		assert(((Square) dungeon.getSquareAt(5, 3, 0)).getBorderAt(Direction.EAST).hasOnlyOneSquareAttached());
	}

	@Test
	public void testIsConnected() throws IllegalArgumentException, IllegalSquareException {
		dungeon.addSquare(square1, 1, 2, 0);
		dungeon.addSquare(square2, 1, 3, 0);
		((Square) dungeon.getSquareAt(1, 2, 0)).changeBorderAt(Direction.NORTH, new Open(square1,square2));
		assert(Dungeon.isConnected(square1, square2));
	}

}
